package com.example.demo.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.example.demo.dao.IBaseDao;
import com.example.demo.service.IBaseService;

/**
 * 服务基类
 * @author Administrator
 * @param <T>
 */
public abstract class BaseServiceImpl<T> implements IBaseService<T>{
	private static Logger logger = Logger.getLogger(BaseServiceImpl.class);

	protected abstract IBaseDao<T> getMapper();

	@Override
	public int insert(T entity) throws Exception {
		int result = getMapper().insert(entity);
		return result;
	}

	/**
	 * 插入数据(选择性地)
	 * 
	 * @param entity @return @throws Exception @throws
	 */
	public int insertSelective(T entity) throws Exception {
		int result = getMapper().insertSelective(entity);
		return result;
	}

	@Override
	public int updateByPrimaryKey(T entity) throws Exception {
		int result = getMapper().updateByPrimaryKey(entity);
		return result;
	}

	/**
	 * 更新数据(选择性地)
	 * 
	 * @param entity @return @throws Exception @throws
	 */
	public int updateByPrimaryKeySelective(T entity) throws Exception {
		int result = getMapper().updateByPrimaryKeySelective(entity);
		return result;
	}

	@Override
	public int deleteByPrimaryKey(int id) throws Exception {
		int result = getMapper().deleteByPrimaryKey(id);
		return result;
	}

	@Override
	public int delete(T entity) throws Exception {
		int result = getMapper().delete(entity);
		return result;
	}

	@Override
	public T selectByPrimaryKey(int id) {
		T obj = null;
		try {
			obj = getMapper().selectByPrimaryKey(id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return obj;
	}

	@Override
	public T getOne(T entity) {
		T obj = null;
		try {
			obj = getMapper().getOne(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return obj;
	}

	@Override
	public Object getObject(Object obj) {
		Object result = null;
		try {
			result = getMapper().getObject(obj);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}



	@Override
	public int getCount(T entity) {
		int result = 0;
		try {
			result = getMapper().getCount(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	public List<T> getAll() {
		List<T> list = null;
		try {
			list = getMapper().getAll();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return list;
	}

	public List<T> getAllBySelect(T entity) {
		List<T> list = null;
		try {
			list = getMapper().getAllBySelect(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return list;
	}
}
